/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-30
*/
package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Freight;
import com.rzico.basics.entity.Product;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.FreightService;
import com.rzico.basics.service.ProductService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运费模板控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-30
 */
@Api(description = "运费物流")
@RestController
@RequestMapping("/freight")
public class FreightController extends BaseController {

    @Autowired
    private FreightService freightService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private ProductService productService;

    /**
     * 计算运费
     *
     * @param productId
     * @return
     */

    @ApiOperation("计算运费")
    @PostMapping("/calculate")
    public CommResult<BigDecimal> calculate(Long productId,Integer areaId) {
        Product product = productService.selectByPrimaryKey(productId);
        if (product==null) {
            return CommResult.error("无效商品id");
        }

        BigDecimal amount = BigDecimal.ZERO;

        if (product.getFreightId()!=null) {
            Freight freight= freightService.findById(product.getFreightId());
            if (freight!=null) {
                amount = freightService.calculate(freight,BigDecimal.ONE,product.getWeight(),areaId);
            }
        }

        Map<String,Object> data = new HashMap<>();
        data.put("freight",amount);
        data.put("estimateDate", DateUtils.addDays(new Date(),3));

        return CommResult.success(data);
    }

}
